



import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class Encrypt3 {

	private String sk = "hrbesd";
	private String password;
	private KeyGenerator kgen;
	private SecretKey secretKey;
	private byte[] enCodeFormat;
	private SecretKeySpec key;
	private Cipher cipher;
	private SecureRandom secureRandom;

	public Encrypt3() {
		try {
			password = getMD5(sk);
			System.out.println(password);
			kgen = KeyGenerator.getInstance("AES");
			secureRandom = SecureRandom.getInstance("SHA1PRNG");
			secureRandom.setSeed(password.getBytes());
			kgen.init(128, secureRandom);
			secretKey = kgen.generateKey();
			enCodeFormat = secretKey.getEncoded();
			key = new SecretKeySpec(enCodeFormat, "AES");
			cipher = Cipher.getInstance("AES");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * 获取SK
	 *
	 * @param plainText
	 * @return
	 */
	private static String getMD5(String plainText) {
		byte[] secretBytes = null;
		try {
			secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes());
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("没有这个md5算法！");
		}
		String md5code = new BigInteger(1, secretBytes).toString(16);
		for (int i = 0; i < 32 - md5code.length(); i++) {
			md5code = "0" + md5code;
		}
		return md5code;
	}

	/**
	 * AES加密字符串
	 *
	 * @param content
	 *            加密内容
	 *
	 * @return 密文
	 */
	public byte[] encrypt(String content) {
		try {
			byte[] byteContent = content.getBytes("utf-8");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] result = cipher.doFinal(byteContent);
			return result;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 返回16进制加密结果
	 * 
	 * @param content
	 * @return
	 */
	public String encryptFor16(String content) {
		return parseByte2HexStr(encrypt(content));
	}

	/**
	 * 解密AES加密过的字符串
	 *
	 * @param content
	 *            解密密文
	 *
	 * @return 明文
	 */
	public byte[] decrypt(byte[] content) {
		try {
			cipher.init(Cipher.DECRYPT_MODE, key);// 初始化为解密模式的密码器
			byte[] result = cipher.doFinal(content);
			return result; // 明文

		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalBlockSizeException e) {
			e.printStackTrace();
		} catch (BadPaddingException e) {
			e.printStackTrace();
		}
		return null;
	}

	public String decryptFor16(String content) {
		return new String(decrypt(parseHexStr2Byte(content)));
	}

	/**
	 * 将二进制转换成16进制
	 * 
	 * @param buf
	 * @return
	 */
	private static String parseByte2HexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < buf.length; i++) {
			String hex = Integer.toHexString(buf[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * 将16进制转换为二进制
	 * 
	 * @param hexStr
	 * @return
	 */
	private static byte[] parseHexStr2Byte(String hexStr) {
		if (hexStr.length() < 1)
			return null;
		byte[] result = new byte[hexStr.length() / 2];
		for (int i = 0; i < hexStr.length() / 2; i++) {
			int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
			int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
			result[i] = (byte) (high * 16 + low);
		}
		return result;
	}

	public static void main(String[] args) {

		String content = "FIE3";
		Encrypt3 e1, e2;

		e1 = new Encrypt3();
		e2 = new Encrypt3();
		System.out.println("加密之前：" + content);

		// 加密
		// byte[] encrypt = e1.encrypt(content);
		String encrypt = e1.encryptFor16(content);
		System.out.println("加密后的内容：" + encrypt);

		// 解密
		//byte[] decrypt = e2.decrypt(encrypt);
		String decrypt = e2.decryptFor16("CF1EF5D48788D8CAFF03C6C8A01DFAAA");
		System.out.println("解密后的内容：" + decrypt);

	}
}